# -*- coding: utf-8 -*-
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
from sklearn.model_selection import cross_val_score

from sklearn.metrics import classification_report


import numpy as np
from matplotlib import pyplot as plt


iris = load_iris()
x = iris.data
y = iris.target
print(x.shape)

#划分训练集与验证集
X_train, X_test, y_train, y_test = train_test_split(x, y, test_size=0.4, random_state=0)


from sklearn.svm import SVC
clf = SVC(decision_function_shape='ovo')


clf.fit(X_train, y_train)
print(clf.support_vectors_)


y_predict = clf.predict(X_test)
#模型的验证报告
print(classification_report(y_test, y_predict))



linear_svc = SVC(kernel="linear")
#linear_svc.fit(X_train, y_train)
rbf_svc = SVC(kernel="rbf")
#rbf_svc.fit(X_train, y_train)

print('cross val')
print(cross_val_score(linear_svc, x, y, cv=5))
print(cross_val_score(rbf_svc, x, y, cv=5))

from sklearn.ensemble import BaggingClassifier

clf_bagging = BaggingClassifier(estimator=SVC(),
                                n_estimators=3, 
                                random_state=0).fit(X_train, y_train)
y_predict = clf_bagging.predict(X_test)
print(classification_report(y_test, y_predict))

from sklearn.ensemble import RandomForestClassifier
clf = RandomForestClassifier(max_depth=2, random_state=0)

from sklearn.ensemble import AdaBoostClassifier
clf = AdaBoostClassifier(n_estimators=100, random_state=0)


from sklearn.ensemble import RandomForestClassifier
from sklearn.svm import LinearSVC
from sklearn.linear_model import LogisticRegression
from sklearn.preprocessing import StandardScaler
from sklearn.pipeline import make_pipeline
from sklearn.ensemble import StackingClassifier

estimators = [
    ('rf', RandomForestClassifier(n_estimators=10, random_state=42)),
    ('svr', make_pipeline(StandardScaler(),
                          LinearSVC(dual="auto", random_state=42)))
]
clf = StackingClassifier(
    estimators=estimators, final_estimator=LogisticRegression()
)

print(clf.fit(X_train, y_train).score(X_test, y_test))